package com.jifen.goods.service;

import com.jifen.config.Global;
import com.jifen.goods.dao.GoodsCateDao;
import com.jifen.goods.dao.GoodsInfoDao;
import com.jifen.goods.entity.GoodsCate;
import com.jifen.goods.entity.GoodsInfo;
import com.jifen.goods.vo.GoodsParam;
import com.jifen.goods.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsService {

    @Autowired
    GoodsInfoDao goodsInfoDao;
    @Autowired
    GoodsCateDao goodsCateDao;

    /**
     * 查询所有分类
     * @return
     */
    public List<GoodsCate> getCate(){
        return goodsCateDao.getAll();
    }

    /**
     * 查询商品
     * @param param
     * @return
     */
    public List<GoodsVo> search(GoodsParam param){
        List<GoodsVo> voList = new ArrayList<>();
        param.setPageNo((param.getPageNo() - 1) * param.getPageSize());
        List<GoodsInfo> list = goodsInfoDao.selectPage(param);
        for (int i = 0; i < list.size(); i++) {
            GoodsInfo goods = list.get(i);
            GoodsVo vo = new GoodsVo();
            vo.setId(goods.getId());
            vo.setName(goods.getName());
            vo.setPrice(goods.getPrice());
            vo.setCoverPic(goods.getCoverPic());
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    public GoodsVo detail(int id) {
        GoodsVo vo = new GoodsVo();
        GoodsInfo goods = goodsInfoDao.selectByPrimaryKey(id);
        vo.setId(goods.getId());
        vo.setName(goods.getName());
        vo.setPrice(goods.getPrice());
        vo.setCoverPic(goods.getCoverPic());
        String[] slidePics = goods.getSlidePic().split(",");
        String[] detailPics = goods.getDetailPic().split(",");
        for (int i = 0; i < slidePics.length; i++) {
            slidePics[i] = Global.getConfig("cdnUrl")+slidePics[i];
        }
        for (int i = 0; i < detailPics.length; i++) {
            detailPics[i] = Global.getConfig("cdnUrl")+detailPics[i];
        }
        vo.setSlidePic(slidePics);
        vo.setDetailPic(detailPics);
        return vo;
    }
}
